//
//  UserAccount.swift
//  weibo22
//
//  Created by keshuaiguo on 16/1/18.
//  Copyright © 2016年 keshuaiguo. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding{

    //用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    
    //access_token的生命周期，单位是秒数。过多少秒过期
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //token 过期日期<开发者过期日期5年,普通->3天>
    var expires_date: NSDate?
    
    //当前授权用户的UID。
    var uid: String?
    //用户名称
    var name: String?
    //用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    //KVC设置初始值
    init(dict:[String : AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    //过滤key
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    //重写对象的描述信息
    override var description: String {
        
        let keys = ["access_token","expires_in","uid","name","avatar_large","expires_date"]
        
        let dict = self.dictionaryWithValuesForKeys(keys)
        
        return dict.debugDescription
    }
    
    //保存自定义对象
    func saveAccount() {
    //保存到沙盒中
        //1.拼接路径
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
       
        //2.将对象保存到沙盒中
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        
//        print(path)
    }
    //获取磁盘中的归档对象
    class func loadUserAccount() -> UserAccount? {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as
            NSString).stringByAppendingPathComponent("account.plist")
        
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
            //并且 token 没有过期 最后才会返回一个 用户账户对象
            //NSDate() 获取当前日期
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                
                return account
            }
        }
        
        return nil
    }
    
    
    //实现NSCoding的协议方法
    //解归档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token")as? String
        //基本数据类型,可直接归档具体类型
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        name = aDecoder.decodeObjectForKey("name")as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large")as? String
        
    }
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
}
